package cn.shyouchi.wuguanjia.android.ui.statistic;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.statistic.page.MonthStatisticEntity;
import cn.shyouchi.wuguanjia.android.widget.CircularProgressBar;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static cn.shyouchi.wuguanjia.android.R.id.item_group_rank_container;

public class SignStatisticProfileActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_group_rank_title)
    TextView mTvGroupRankTitle;
    @BindView(R.id.tv_group_rank_info)
    TextView mTvGroupRankInfo;
    @BindView(item_group_rank_container)
    LinearLayout mItemGroupRankContainer;
    @BindView(R.id.tv_refuel_rank_title)
    TextView mTvRefuelRankTitle;
    @BindView(R.id.tv_refuel_rank_info)
    TextView mTvRefuelRankInfo;
    @BindView(R.id.item_group_refuel_container)
    LinearLayout mItemGroupRefuelContainer;
    @BindView(R.id.tv_group_attendance_title)
    TextView mTvGroupAttendanceTitle;
    @BindView(R.id.tv_sign_statistic_chidao)
    TextView mTvSignStatisticChidao;
    @BindView(R.id.tv_sign_statistic_kuanggong)
    TextView mTvSignStatisticKuanggong;
    @BindView(R.id.tv_sign_statistic_zaotui)
    TextView mTvSignStatisticZaotui;
    @BindView(R.id.tv_sign_statistic_jiaban)
    TextView mTvSignStatisticJiaban;
    @BindView(R.id.tv_sign_statistic_waichu)
    TextView mTvSignStatisticWaichu;
    @BindView(R.id.tv_sign_statistic_shixi)
    TextView mTvSignStatisticShixi;
    @BindView(R.id.tv_sign_statistic_qingjia)
    TextView mTvSignStatisticQingjia;
    @BindView(R.id.tv_sign_statistic_jiediao)
    TextView mTvSignStatisticJiediao;
    @BindView(R.id.tv_year)
    TextView mTvYear;
    @BindView(R.id.pro_rate)
    CircularProgressBar mProRate;
    @BindView(R.id.tv_rate)
    TextView mTvRate;
    @BindView(R.id.tv_newly_num)
    TextView mTvNewlyNum;
    @BindView(R.id.tv_leave_num)
    TextView mTvLeaveNum;
    @BindView(R.id.activity_sign_statistic_profile)
    LinearLayout mActivitySignStatisticProfile;


    AttendanceApi mAttendanceApi;

    String date;

    boolean isYear = false;//是否是年度排名


    String projectId;
    @BindView(R.id.tv_group_synthesis_title)
    TextView mTvGroupSynthesisTitle;
    @BindView(R.id.tv_group_synthesis_info)
    TextView mTvGroupSynthesisInfo;
    @BindView(R.id.item_group_synthesis_container)
    LinearLayout mItemGroupSynthesisContainer;

    @Override
    public int initContentView() {
        return R.layout.activity_sign_statistic_profile;
    }

    @Override
    public void initInjector() {
        mAttendanceApi = new AttendanceApi(mContext);
        date = getIntent().getStringExtra("date");
        isYear = getIntent().getBooleanExtra("year", false);
        projectId = getIntent().getStringExtra("projectId");

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(date + "考勤统计");
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        if (isYear) {
            mTvYear.setText(date);
            mTvGroupSynthesisTitle.setText("年度综合排行");
            mTvGroupRankTitle.setText("年度考勤排名");
            mTvRefuelRankTitle.setText("年度加油榜");
            mTvGroupAttendanceTitle.setText("年度团体考勤情况");

        } else {
            mTvYear.setText(date);

            mTvGroupSynthesisTitle.setText("月度综合排行");
            mTvGroupRankTitle.setText("月度考勤排名");
            mTvRefuelRankTitle.setText("月度加油榜");
            mTvGroupAttendanceTitle.setText("月度团体考勤情况");

        }

        fetchData();


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick({R.id.tv_group_rank_info, R.id.tv_refuel_rank_info, R.id.tv_sign_statistic_chidao, R.id.tv_sign_statistic_kuanggong, R.id.tv_sign_statistic_zaotui, R.id.tv_sign_statistic_jiaban, R.id.tv_sign_statistic_waichu, R.id.tv_sign_statistic_shixi, R.id.tv_sign_statistic_qingjia, R.id.tv_sign_statistic_jiediao, R.id.tv_newly_num, R.id.tv_leave_num,R.id.tv_group_synthesis_info})
    public void onClick(View view) {

        Intent intent = new Intent();
        intent.putExtra("date", date);
        intent.putExtra("year", isYear);
        intent.putExtra("projectid", projectId);

        switch (view.getId()) {
            case R.id.tv_group_rank_info:

                intent.setClass(mContext, StatisticGroupRankActivity.class);

                break;
            case R.id.tv_refuel_rank_info:
                intent.setClass(mContext, StatisticGroupRefuelActivity.class);

                break;
            case R.id.tv_sign_statistic_chidao:
                intent.putExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_LATE);

                intent.setClass(mContext, StatisticMonthSignDetailActivity.class);
                break;
            case R.id.tv_sign_statistic_kuanggong:
                intent.putExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_ABSE);
                intent.setClass(mContext, StatisticMonthSignDetailActivity.class);
                break;
            case R.id.tv_sign_statistic_zaotui:
                intent.putExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_EARLY);
                intent.setClass(mContext, StatisticMonthSignDetailActivity.class);
                break;
            case R.id.tv_sign_statistic_jiaban:
                intent.putExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_OVER);
                intent.setClass(mContext, StatisticMonthSignDetailActivity.class);
                break;
            case R.id.tv_sign_statistic_waichu:
                intent.putExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_OUT);
                intent.setClass(mContext, StatisticMonthSignDetailActivity.class);
                break;
            case R.id.tv_sign_statistic_shixi:
                intent.putExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_PRAC);
                intent.setClass(mContext, StatisticMonthSignDetailActivity.class);
                break;
            case R.id.tv_sign_statistic_qingjia:
                intent.putExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_LEAVE);
                intent.setClass(mContext, StatisticMonthSignDetailActivity.class);
                break;
            case R.id.tv_sign_statistic_jiediao:
                intent.putExtra("type", StatisticMonthSignDetailAdapter.MONTH_SIGN_TYPE_SECO);
                intent.setClass(mContext, StatisticMonthSignDetailActivity.class);
                break;
            case R.id.tv_newly_num:
                intent.setClass(mContext, NewlyStaffActivity.class);
                break;
            case R.id.tv_leave_num:
                intent.setClass(mContext, LeavingStaffActivity.class);
                break;
            case R.id.tv_group_synthesis_info:
                intent.setClass(mContext, StatisticGroupIntegratedActivity.class);
                break;
        }
        startActivity(intent);
    }


    private void fetchData() {

        if (isYear) {
            mAttendanceApi.getYearStatisticData(date, projectId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<MonthStatisticEntity>(mContext) {
                        @Override
                        public void onNext(MonthStatisticEntity result) {
                            mTvSignStatisticChidao.setText(String.format("%s次", result.getResultMessage().getLate()));
                            mTvSignStatisticKuanggong.setText(String.format("%s次", result.getResultMessage().getAbsenteeism()));
                            mTvSignStatisticZaotui.setText(String.format("%s次", result.getResultMessage().getEarly()));
                            mTvSignStatisticJiaban.setText(String.format("%s次", result.getResultMessage().getOvertime()));
                            mTvSignStatisticWaichu.setText(String.format("%s次", result.getResultMessage().getGo_out()));
                            mTvSignStatisticShixi.setText(String.format("%s次", result.getResultMessage().getPractice()));
                            mTvSignStatisticQingjia.setText(String.format("%s次", result.getResultMessage().getLeave()));
                            mTvSignStatisticJiediao.setText(String.format("%s次", result.getResultMessage().getSeconded()));
                            mTvNewlyNum.setText(String.format("新增人员：%s人", result.getResultMessage().getNew_employees()));
                            mTvLeaveNum.setText(String.format("离职人员：%s人", result.getResultMessage().getOutgoing_employee()));
                            mProRate.setProgress((int) result.getResultMessage().getTurnover_rate());
                            mTvRate.setText(result.getResultMessage().getTurnover_rate() + "%");
                            if (!result.getResultMessage().getYear_rank().isEmpty()) {
                                int p = 0;
                                mItemGroupRankContainer.removeAllViews();
                                for (MonthStatisticEntity.ResultMessageBean.MonthRankBean monthRankBean : result.getResultMessage().getYear_rank()) {
                                    mItemGroupRankContainer.addView(createRankView(monthRankBean, p));
                                    p++;
                                }
                            }
                            if (!result.getResultMessage().getYear_integrated().isEmpty()) {
                                int p = 0;
                                mItemGroupSynthesisContainer.removeAllViews();
                                for (MonthStatisticEntity.ResultMessageBean.MonthIntegratedBean monthRankBean : result.getResultMessage().getYear_integrated()) {
                                    mItemGroupSynthesisContainer.addView(createRankView(monthRankBean, p));
                                    p++;
                                }
                            }
                            if (!result.getResultMessage().getYear_refuel().isEmpty()) {
                                mItemGroupRefuelContainer.removeAllViews();
                                for (MonthStatisticEntity.ResultMessageBean.MonthRefuelBean monthRefuelBean : result.getResultMessage().getYear_refuel()) {
                                    mItemGroupRefuelContainer.addView(createRankView(monthRefuelBean));
                                }
                            }
                        }
                    });
        } else {









            mAttendanceApi.getStatisticData(date, projectId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<MonthStatisticEntity>(mContext) {
                        @Override
                        public void onNext(MonthStatisticEntity result) {
                            mTvSignStatisticChidao.setText(String.format("%s次", result.getResultMessage().getLate()));
                            mTvSignStatisticKuanggong.setText(String.format("%s次", result.getResultMessage().getAbsenteeism()));
                            mTvSignStatisticZaotui.setText(String.format("%s次", result.getResultMessage().getEarly()));
                            mTvSignStatisticJiaban.setText(String.format("%s次", result.getResultMessage().getOvertime()));
                            mTvSignStatisticWaichu.setText(String.format("%s次", result.getResultMessage().getGo_out()));
                            mTvSignStatisticShixi.setText(String.format("%s次", result.getResultMessage().getPractice()));
                            mTvSignStatisticQingjia.setText(String.format("%s次", result.getResultMessage().getLeave()));
                            mTvSignStatisticJiediao.setText(String.format("%s次", result.getResultMessage().getSeconded()));
                            mTvNewlyNum.setText(String.format("新增人员：%s人", result.getResultMessage().getNew_employees()));
                            mTvLeaveNum.setText(String.format("离职人员：%s人", result.getResultMessage().getOutgoing_employee()));
                            mProRate.setProgress((int) result.getResultMessage().getTurnover_rate());
                            mTvRate.setText(result.getResultMessage().getTurnover_rate() + "%");

                            if (!result.getResultMessage().getMonth_rank().isEmpty()) {
                                mItemGroupRankContainer.removeAllViews();
                                int p = 0;
                                for (MonthStatisticEntity.ResultMessageBean.MonthRankBean monthRankBean : result.getResultMessage().getMonth_rank()) {
                                    mItemGroupRankContainer.addView(createRankView(monthRankBean, p));
                                    p++;
                                }
                            }

                            if (!result.getResultMessage().getMonth_refuel().isEmpty()) {
                                mItemGroupRefuelContainer.removeAllViews();
                                for (MonthStatisticEntity.ResultMessageBean.MonthRefuelBean monthRefuelBean : result.getResultMessage().getMonth_refuel()) {
                                    mItemGroupRefuelContainer.addView(createRankView(monthRefuelBean));
                                }
                            }

                            if (!result.getResultMessage().getMonth_integrated().isEmpty()) {
                                mItemGroupSynthesisContainer.removeAllViews();
                                int p = 0;
                                for (MonthStatisticEntity.ResultMessageBean.MonthIntegratedBean monthRefuelBean : result.getResultMessage().getMonth_integrated()) {
                                    mItemGroupSynthesisContainer.addView(createRankView(monthRefuelBean,p));
                                    p++;
                                }
                            }


                        }
                    });
        }


    }


    private View createRankView(MonthStatisticEntity.ResultMessageBean.MonthIntegratedBean monthRankBean, int p) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.layout_statistic_rank_person_item, null);
        TextView tvName = (TextView) v.findViewById(R.id.tv_user_name);
        ImageView img = (ImageView) v.findViewById(R.id.img_flag);
        tvName.setText(monthRankBean.getName());
        ImageView ivHeader = (ImageView) v.findViewById(R.id.img_user_header);
        loadHead(Global.BASE_URL + monthRankBean.getId_card_head_image(), ivHeader);

        if (p == 0) {
            img.setVisibility(View.VISIBLE);
            img.setImageResource(R.mipmap.ic_gold);

        } else if (p == 1) {
            img.setVisibility(View.VISIBLE);
            img.setImageResource(R.mipmap.ic_silver);
        } else if (p == 2) {
            img.setVisibility(View.VISIBLE);
            img.setImageResource(R.mipmap.ic_copper);
        }

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
        v.setLayoutParams(lp);
        return v;
    }
    private View createRankView(MonthStatisticEntity.ResultMessageBean.MonthRankBean monthRankBean, int p) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.layout_statistic_rank_person_item, null);
        TextView tvName = (TextView) v.findViewById(R.id.tv_user_name);
        ImageView img = (ImageView) v.findViewById(R.id.img_flag);
        tvName.setText(monthRankBean.getName());
        ImageView ivHeader = (ImageView) v.findViewById(R.id.img_user_header);
        loadHead(Global.BASE_URL + monthRankBean.getId_card_head_image(), ivHeader);

        if (p == 0) {
            img.setVisibility(View.VISIBLE);
            img.setImageResource(R.mipmap.ic_gold);

        } else if (p == 1) {
            img.setVisibility(View.VISIBLE);
            img.setImageResource(R.mipmap.ic_silver);
        } else if (p == 2) {
            img.setVisibility(View.VISIBLE);
            img.setImageResource(R.mipmap.ic_copper);
        }

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
        v.setLayoutParams(lp);
        return v;
    }

    private View createRankView(MonthStatisticEntity.ResultMessageBean.MonthRefuelBean monthRefuelBean) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.layout_statistic_rank_person_item, null);
        TextView tvName = (TextView) v.findViewById(R.id.tv_user_name);
        tvName.setText(monthRefuelBean.getName());

        ImageView ivHeader = (ImageView) v.findViewById(R.id.img_user_header);

        loadHead(Global.BASE_URL + monthRefuelBean.getId_card_head_image(), ivHeader);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
        v.setLayoutParams(lp);

        return v;
    }

    private void loadHead(String path, ImageView imageView) {
        Glide.with(mContext)
                .load(path)
                .placeholder(R.mipmap.ic_default_header)
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
